package com.gking.centerSystem.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.gking.centerSystem.common.Result;
import com.gking.centerSystem.entity.Roles;
import com.gking.centerSystem.service.Roles.RolesService;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Objects;

@Slf4j
@RestController
@RequestMapping("/roles")
public class RolesController {

    @Autowired
    private RolesService rolesService;

    /**
     * 获取角色列表
     *
     * @param roles 筛选条件
     * @param size  分页大小
     * @param page  分页页码
     * @return s
     */
    @RequiresPermissions("CS:manager:get:roles")
    @PostMapping("/rolesList")
    public Result<Page> getRolesList(@RequestBody Roles roles,
                                     @RequestParam Integer size, @RequestParam Integer page) {

        String sortId = roles.getSortId();
        Integer isDefault = roles.getIsDefault();
        String realName = roles.getRealName();

        return rolesService.getRolesList(sortId, isDefault, realName, size, page);
    }

    /**
     * 创建角色或者修改角色信息
     *
     * @param role 角色信息
     * @return s
     */
    @RequiresPermissions("CS:manager:set:roles")
    @PostMapping("/setRole")
    public Result<String> setRole(@RequestBody Roles role) {

        String roleId = role.getId(); // 可选参数，修改角色时可带上，创建时无需
        String name = role.getName();
        String realName = role.getRealName();
        String description = role.getDescription();
        String sortId = role.getSortId();
        Integer isDefault = role.getIsDefault();

        return rolesService.setRole(name, realName, description, sortId, isDefault, roleId);
    }

    /**
     * 批量删除角色
     *
     * @param roleIdList 角色id列表
     * @return s
     */
    @RequiresPermissions("CS:manager:set:roles")
    @PostMapping("/deleteRoles")
    public Result<String> deleteRoles(@RequestBody List<String> roleIdList) {
        return rolesService.deleteRoles(roleIdList);
    }

    /**
     * 批量更新角色状态
     *
     * @param roleIdList 角色id 列表
     * @param status     状态 0启用，1禁用
     * @return s
     */
    @RequiresPermissions("CS:manager:set:roles")
    @PostMapping("/updateRolesStatus")
    public Result<String> updateRolesStatus(@RequestBody List<String> roleIdList, @RequestParam Integer status) {
        return rolesService.updateRolesStatus(roleIdList, status);
    }


}
